{extend name="common@base/common" /}

{block name="body"}
<style>
    .list_tr_tr{
        font-size: 12px;
    }
    .move-icon{
        width: 30px;
        float: left;
    }
</style>
	<!-- 标签页导航 -->
	<div class="wrap">
		<ul class="tab-nav nav">
			<li data-tab="tab1" class="current"><a href="javascript:void(0);">基 础</a></li>
			<li data-tab="tab2"><a href="javascript:void(0);">设 计</a></li>
			<li data-tab="tab3"><a href="javascript:void(0);">列表定义</a></li>
            <li><a href="{:U('admin/attribute/index','model_id='.$info['id'])}">字段管理</a></li>
		</ul>

		<div class="tab-content">
			<!-- 表单 -->
			<form id="form" action="{:U('update')}" method="post" class="form-horizontal doc-modal-form">
				<!-- 基础 -->
				<div id="tab1" class="tab-pane in tab1">
					<div class="form-item cf">
						<label class="item-label">模型标识<span class="check-tips">（请输入文档模型标识）</span></label>
						<div class="controls">
							<input type="hidden" name="name" value="{$info.name}">{$info.name}
						</div>
					</div>
					<div class="form-item cf">
						<label class="item-label">模型名称<span class="check-tips">（请输入模型的名称）</span></label>
						<div class="controls">
							<input type="text" class="text input-large" name="title" value="{$info.title}">
						</div>
					</div>
					<div class="form-item cf">
						<label class="item-label">所属应用</label>
						<div class="controls">
                                {volist name="list" id="m"}
								{if condition="$info['addon'] eq $key"} <input type="hidden" name="addon" value="{$key}">{$m} {/if}
                                {/volist}
						</div>
					</div>
                    <div class="form-item cf">
                        <label class="item-label">继承模型</label>
                        <div class="controls"><input type="hidden" name="inherit" value="{$info.inherit}">{$info.inherit_title}</div>
                    </div>
                </div>

				<div id="tab2" class="tab-pane tab2">
					<div class="form-item cf">
					<label class="item-label">新增按钮</label>
					<div class="controls">
                    <input type="radio" name="add_button" value="1" {if condition="$info['add_button']==1"}checked{/if} > 显示
                    <input type="radio" name="add_button" value="0" {if condition="$info['add_button']==0"}checked{/if} > 隐藏
					</div>
				</div>
                <div class="form-item cf">
					<label class="item-label">批量删除按钮</label>
					<div class="controls">
                           <input type="radio" name="del_button" value="1" {if condition="$info['del_button']==1"}checked{/if} > 显示
                           <input type="radio" name="del_button" value="0" {if condition="$info['del_button']==0"}checked{/if} > 隐藏
					</div>
				</div>
                <div class="form-item cf">
					<label class="item-label">全选复选框</label>
					<div class="controls">
                           <input type="radio" name="check_all" value="1" {if condition="$info['check_all']==1"}checked{/if} > 显示
                           <input type="radio" name="check_all" value="0" {if condition="$info['check_all']==0"}checked{/if} > 隐藏
					</div>
				</div>
                <div class="form-item cf">
					<label class="item-label">搜索功能</label>
					<div class="controls">
                           <input type="radio" name="search_button" value="1" {if condition="$info['search_button']==1"}checked{/if} > 显示
                           <input type="radio" name="search_button" value="0" {if condition="$info['search_button']==0"}checked{/if} > 隐藏
					</div>
				</div>
				<div class="form-item cf">
					<label class="item-label">默认搜索字段<span class="check-tips">（默认列表模板的默认搜索项）</span></label>
					<div class="controls">
						<input type="text" class="text input-large" name="search_key" value="{$info.search_key}">
					</div>
				</div>
				<div class="form-item cf">
					<label class="item-label">列表每页条数<span class="check-tips">（默认列表模板的分页属性）</span></label>
					<div class="controls">
						<input type="text" class="text input-small" name="list_row" value="{$info.list_row|default=20}">
					</div>
				</div>
			</div>
            <div id="tab3" class="tab-pane tab3">
				<div class="form-item cf">
					<label class="item-label">列表定义<span class="check-tips">（默认列表模板的展示规则）</span></label>
					<div style="margin: 15px 0; " class="specTable data-table">
						<table cellspacing="1" cellpadding="0">
							<thead>
								<tr>
                                    <th align="center" style="width: 150px">数据源</th>
									<th align="center">字段</th>
                                    <th align="center" style="width: 150px">列名</th>
									<th align="center" style="width: 100px" class="show-help">宽度 <i class="layui-icon layui-icon-help" style="font-size: 20px; color: #999;"></i>  </th>
									<th align="center" style="width: 150px">排序</th>
                                    <th align="center" style="width: 150px">可编辑</th>
									<th align="center" style="width: 150px">操作</th>
								</tr>
							</thead>
							<tbody id="list_data_tbody">
								{volist name="list_grid" id="cd" key="sort"}
                                {php}$name = $key;if($name=='ids'){ $cd['come_from']=1; }{/php}
								<tr class="list_tr" rel="{$sort}">
									<td align="center">
                                    <select name="come_from[{$sort}]" class="select_type">
											<option value="0"{eq name="cd['come_from']" value="0"}selected{/eq} >表字段 </option>
											<option value="1"{eq name="cd['come_from']" value="1"}selected{/eq} >管理操作 </option>
									</select></td>
                                    <td align="center">
                                    <div class="select_field">
                                          <select name="field[{$sort}]" class="select_field_event">
                                              {volist name="fields" id="vo"}
                                              <option value="{$key}" {eq name="name" value="$key"}selected{/eq}  rel="{$vo.type}">{$vo.title}</option>
                                              {/volist}
                                          </select>
                                    </div>
                                    <div class="custom_field">
                                    <table cellspacing="1" cellpadding="0">
                                        <tbody>
                                            {volist name="cd.href" id="vo"}
                                            <tr class="list_tr_tr">
                                                <td align="center" style="width: 100px"><input type="text" value="{$vo.title}"  class="form-control" name="url_title[{$sort}][]" style="width:100px"></td>
                                                <td align="center" style="width: 150px"><input type="text" value="{$vo.url}"  class="form-control" name="url_url[{$sort}][]" style="width:150px"></td>
                                                <td><a href="javascript:void(0);" onclick="titles(this)">高级设置</a>
                                                    <input type="hidden" value="{$vo.show_set|json_encode|urlencode}" class="show_set" name="show_set[{$sort}][]">
                                                    <input type="hidden" value="{$vo.class|default=''}" class="class" name="class[{$sort}][]">
                                                    <div class="show-tips">{$vo.show}</div>
                                                </td>
                                                <td align="center" style="width: 30px"><div class="move-icon"> <a href="javascript:void(0);" onclick="move_up(this)" title="向上"class="sub_move_up"><i class="layui-icon layui-icon-up"></i></a>
                                                    <a href="javascript:void(0);" onclick="move_down(this)" title="向下"class="sub_move_down"><i class="layui-icon layui-icon-down"></i></a></div></td>
                                                <td style="width: 50px"><a href="javascript:void(0);" onclick="remove_tr(this)">删除</a></td>
                                            </tr>
                                            {/volist}
                                            <tr>
                                                <td colspan="5"><a href="javascript:void(0);" onclick="add_tr_tr(this)">+增加链接</a></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                    </div>
                                    </td>
                                    <td align="center"><input type="text" value="{$cd.title}" class="form-control attr_title" name="attr_title[{$sort}]"></td>
									<td align="center"><input type="text" value="{$cd.width}" class="form-control" name="width[{$sort}]" placeholder="自动" style="width:50px"> </td>
									<td><div class="sort-row"><input type="radio" name="is_sort[{$sort}]" value="0" {if condition="$cd['is_sort']==0"}checked{/if} > 否
                                        <input type="radio" name="is_sort[{$sort}]" value="1" {if condition="$cd['is_sort']==1"}checked{/if} > 是</div></td>
                                    <td><div class="edit-row"><input type="radio" name="can_edit[{$sort}]" value="0" {if condition="$cd['can_edit']==0"}checked{/if} > 否
                                        <input type="radio" name="can_edit[{$sort}]" value="1" {if condition="$cd['can_edit']==1"}checked{/if} > 是</div></td>
									<td>
                                        <div class="move-icon"><a href="javascript:void(0);" onclick="move_up(this)" title="向上"class="move_up"><i class="layui-icon layui-icon-up"></i> </a>
                                            <a href="javascript:void(0);" onclick="move_down(this)" title="向下"class="move_down"><i class="layui-icon layui-icon-down"></i></a></div>&nbsp;&nbsp;&nbsp;
                                    <a href="javascript:void(0);" onclick="remove_tr(this)">删除</a></td>
								</tr>
								{/volist}
								<tr class="more_tr">
									<td colspan="7"><a href="javascript:add_tr()">+增加新列</a></td>
								</tr>
							</tbody>
						</table>
					</div>
				</div>
			</div>

			<!-- 按钮 -->
			<div class="form-item cf">
				<label class="item-label"></label>
				<div class="controls edit_sort_btn">
					<input type="hidden" name="id" value="{$info['id']|default=0}" />
					<button class="btn submit-btn ajax-post no-refresh" type="submit"
						target-form="form-horizontal">确 定</button>
					<button class="btn btn-return">返 回</button>
				</div>
			</div>
		</form>
	</div>
</div>
<table style="display: none">
	<tr id="default_tr">
        <td align="center">
        <select name="come_from[sort_id]" class="select_type">
                <option value="0" selected>表字段</option>
                <option value="1">管理操作</option>
        </select>
        </td>
        <td align="center">
        <div class="select_field">
        <select name="field[sort_id]" class="select_field_event">
                {volist name="fields" id="vo"}
                <option value="{$key}" rel="{$vo.type}">{$vo.title}</option>
                {/volist}
        </select>
        </div>
        <div class="custom_field">
        <table cellspacing="1" cellpadding="0">
            <tbody>
                <tr class="list_tr_tr">
                    <td align="center" style="width: 100px"><input type="text" value="编辑"  class="form-control" name="url_title[sort_id][]" style="width:100px"></td>
                    <td align="center" style="width: 150px"><input type="text" value="[EDIT]"  class="form-control" name="url_url[sort_id][]" style="width:150px"></td>
                    <td><a href="javascript:void(0);" onclick="titles(this)">高级设置</a>
                        <input type="hidden" value="" class="show_set" name="show_set[sort_id][]">
                        <input type="hidden" value="" class="class" name="class[sort_id][]">
                        <div class="show-tips"></div>
                    </td>
                    <td align="center" style="width: 30px"><div class="move-icon"><a href="javascript:void(0);" onclick="move_up(this)" title="向上"class="sub_move_up"><i class="layui-icon layui-icon-up"></i></a>
                        <a href="javascript:void(0);" onclick="move_down(this)" title="向下"class="sub_move_down"><i class="layui-icon layui-icon-down"></i></a></div></td>
                    <td style="width: 50px"><a href="javascript:void(0);" onclick="remove_tr(this)">删除</a></td>
                </tr>
                <tr class="list_tr_tr">
                    <td align="center" style="width: 100px"><input type="text" value="删除"  class="form-control" name="url_title[sort_id][]" style="width:100px"></td>
                    <td align="center" style="width: 150px"><input type="text" value="[DELETE]"  class="form-control" name="url_url[sort_id][]" style="width:150px"></td>
                    <td><a href="javascript:void(0);" onclick="titles(this)">高级设置</a>
                        <input type="hidden" value="" class="show_set" name="show_set[sort_id][]">
                        <input type="hidden" value="" class="class" name="class[sort_id][]">
                        <div class="show-tips"></div>
                    </td>
                    <td align="center" style="width: 30px"><div class="move-icon"><a href="javascript:void(0);" onclick="move_up(this)" title="向上"class="sub_move_up"><i class="layui-icon layui-icon-up"></i></a>
                        <a href="javascript:void(0);" onclick="move_down(this)" title="向下"class="sub_move_down"><i class="layui-icon layui-icon-down"></i></a></div></td>
                    <td style="width: 50px"><a href="javascript:void(0);" onclick="remove_tr(this)">删除</a></td>
                </tr>
                <tr class="list_tr_tr">
                    <td align="center" style="width: 100px"><input type="text" value="预览"  class="form-control" name="url_title[sort_id][]" style="width:100px"></td>
                    <td align="center" style="width: 150px"><input type="text" value="preview?id=[id]"  class="form-control" name="url_url[sort_id][]" style="width:150px"></td>
                    <td><a href="javascript:void(0);" onclick="titles(this)">高级设置</a>
                        <input type="hidden" value="" class="show_set" name="show_set[sort_id][]">
                        <input type="hidden" value="" class="class" name="class[sort_id][]">
                        <div class="show-tips"></div>
                    </td>
                    <td align="center" style="width: 30px"><div class="move-icon"><a href="javascript:void(0);" onclick="move_up(this)" title="向上"class="sub_move_up"><i class="layui-icon layui-icon-up"></i></a>
                        <a href="javascript:void(0);" onclick="move_down(this)" title="向下"class="sub_move_down"><i class="layui-icon layui-icon-down"></i></a></div></td>
                    <td style="width: 50px"><a href="javascript:void(0);" onclick="remove_tr(this)">删除</a></td>
                </tr>

                <tr>
                    <td colspan="5"><a href="javascript:void(0);" onclick="add_tr_tr(this)">+增加链接</a></td>
                </tr>
            </tbody>
        </table>
        </div>
        </td>
        <td align="center"><input type="text" value=""  class="form-control attr_title" name="attr_title[sort_id]"></td>
        <td align="center"><input type="text" value="" class="form-control" name="width[sort_id]" placeholder="自动" style="width:50px"> </td>
        <td><div class="sort-row"><input type="radio" name="is_sort[sort_id]" value="0" checked> 否<input type="radio" name="is_sort[sort_id]" value="1"> 是</div></td>
        <td><div class="edit-row"><input type="radio" name="can_edit[sort_id]" value="0" checked > 否<input type="radio" name="can_edit[sort_id]" value="1"> 是</div></td>
        <td><div class="move-icon"><a href="javascript:void(0);" onclick="move_up(this)" title="向上"class="move_up"><i class="layui-icon layui-icon-up"></i></a>
            <a href="javascript:void(0);" onclick="move_down(this)" title="向下"class="move_down"><i class="layui-icon layui-icon-down"></i></a></div>&nbsp;&nbsp;&nbsp;
        <a href="javascript:void(0);" onclick="remove_tr(this)">删除</a></td>
	</tr>
    <tr id="default_tr_tr">
    <td align="center" style="width: 100px"><input type="text" value=""  class="form-control" name="url_title[sort_id][]" style="width:100px"></td>
    <td align="center" style="width: 150px"><input type="text" value=""  class="form-control" name="url_url[sort_id][]" style="width:150px"></td>
        <td><a href="javascript:void(0);" onclick="titles(this)">高级设置</a>
            <input type="hidden" value="" class="show_set" name="show_set[sort_id][]">
            <input type="hidden" value="" class="class" name="class[sort_id][]">
            <div class="show-tips"></div>
        </td>
        <td align="center" style="width: 30px"><div class="move-icon"><a href="javascript:void(0);" onclick="move_up(this)" title="向上"class="sub_move_up"><i class="layui-icon layui-icon-up"></i></a>
            <a href="javascript:void(0);" onclick="move_down(this)" title="向下"class="sub_move_down"><i class="layui-icon layui-icon-down"></i></a></div></td>
    <td style="width: 50px"><a href="javascript:void(0);" onclick="remove_tr(this)">删除</a></td>
</tr>
</table>

{/block}
{block name="script"}
<script	type="text/javascript" charset="utf-8">


function init(){
	hide_move()
	hide_sub_move()

	$('.select_type').each(function(){ select_type(this); });
	$('.select_type').change(function(){ select_type(this); });

	$('.select_field_event').each(function(){ select_field_event(this); });
	$('.select_field_event').change(function(){ select_field_event(this); });
}

$(function(){
    showTab()
	//add_tr()
	init()
    $('.show-help').mouseover(function () {
        layer.tips('格式如：10%或10px', '.show-help');
    })
});

//增加问题
var tr_sort_id = 0;
function add_tr(){
	var list_count = 0;
	$('.list_tr').each(function() {
		list_count += 1;
		var sort_id = $(this).attr('rel');
		if(sort_id>tr_sort_id) tr_sort_id = sort_id;
    });

	tr_sort_id += 1;

	re = new RegExp("sort_id", "g");
	str  = $('#default_tr').html().replace(re, tr_sort_id);
	//console.log(str);
	var html = '<tr class="list_tr" rel="'+tr_sort_id+'">'+ str +'</tr>';
	if(list_count==0)
	  $('#list_data_tbody tr').before(html);
	else
	  $('.list_tr:last').after(html);

	init()
}
function add_tr_tr(_this){
    var tr_sort_id = $(_this).parents('.list_tr').attr('rel')

	re = new RegExp("sort_id", "g");
	str  = $('#default_tr_tr').html().replace(re, tr_sort_id);
	console.log(str);
	var html = '<tr class="list_tr_tr" rel="'+tr_sort_id+'">'+ str +'</tr>';

	$(_this).parent().parent().parent().before(html);


	init()
}
//删除问题
function remove_tr(_this){
	$(_this).parent().parent().remove();
	init()
}
//排序--向上
function move_up(obj) {
  var objParentTR = $(obj).parent().parent().parent();
  var prevTR = objParentTR.prev();
  if (prevTR.length > 0) {
	prevTR.insertAfter(objParentTR);
  }
  init()
}
//排序--向下
function move_down(obj) {
  var objParentTR = $(obj).parent().parent().parent();
  var nextTR = objParentTR.next();
  if (nextTR.length > 0) {
	nextTR.insertBefore(objParentTR);
  }
  init()
}
//第一行只显示向下，最后一行只显示向上
function hide_move(){
	$('.move_up').each(function() {
		$(this).show();
    });
	$('.move_down').each(function() {
		$(this).show();
    });
	$('.list_tr:first').find('.move_up').hide();
	$('.list_tr:last').find('.move_down').hide();
}
function hide_sub_move(){
	$('.sub_move_up').each(function() {
		$(this).show();
    });
	$('.sub_move_down').each(function() {
		$(this).show();
    });
	$('.custom_field').each(function() {
          $(this).find('.list_tr_tr:first').find('.sub_move_up').hide();
	      $(this).find('.list_tr_tr:last').find('.sub_move_down').hide();
    });
}

//选择问题类型
function select_type(_this){
	var type = $(_this).val();

	var input = $(_this).parents('.list_tr').find('.attr_title')
	var field_title = $(_this).parents('.list_tr').find('.select_field_event').find("option:selected").text();

	var old_val = input.val()
	if(type==1){
		$(_this).parent().next().find('.select_field').hide();
		$(_this).parent().next().find('.custom_field').show();

        $(_this).parent().parent().find('.sort-row').hide();
        $(_this).parent().parent().find('.edit-row').hide();

		if(old_val==''|| old_val==field_title){
			input.val('操作')
		}
	}else{
		$(_this).parent().next().find('.select_field').show();
		$(_this).parent().next().find('.custom_field').hide();
        $(_this).parent().parent().find('.sort-row').show();
        $(_this).parent().parent().find('.edit-row').show();

		if(old_val=='操作'){
			input.val(field_title)
		}
	}
}
function select_field_event(_this){
    var obj = $(_this).find("option:selected")
	var name = obj.text()
    var rel = obj.attr('rel')
	var input = $(_this).parents('.list_tr').find('.attr_title')
	var old_val = input.val()

	if(old_val==''){
	    input.val(name)
	}else{
		var flat = false
		$(_this).find('option').each(function (){

			if($(this).text()==old_val){
			    flat = true
			}
        });

		if(flat){
			input.val(name)
		}
	}

	if(rel=='bool'){
        $(_this).parents('.list_tr').find('.edit-row').show();
    }else{
        $(_this).parents('.list_tr').find('.edit-row').hide();
        $(_this).parents('.list_tr').find('.edit-row').find("input:radio[value='0']").attr('checked','true');
    }
}
var inputObj
function titles(_this) {
    inputObj = $(_this).parent()
    var field= inputObj.find('.show_set').val()
    var class_val = inputObj.find('.class').val()
    var url = "{:U('showSet',['only_body'=>1,'model_id'=>input('id')])}"
    layer.open({
        type: 2,
        title:"高级设置",
        area: ['600px', '600px'],
        content: url + "/field/" + field+"/class_val/"+class_val
    });
}
function setInput(msg, data, class_val) {
    inputObj.find('.show_set').val(data)
    inputObj.find('.show-tips').html(msg)
    inputObj.find('.class').val(class_val)
}
</script>{/block}
